package com.example.easycharge.service;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.easycharge.entity.Customer;
import com.example.easycharge.notRepEntity.LoginData;
import com.example.easycharge.notRepEntity.SignUpData;
import com.example.easycharge.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CustomerService {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private BillService billService;
    @Autowired
    private ChargeService chargeService;
    @Autowired
    private PileService pileService;
    @Autowired
    private ReportService reportService;

    @Autowired
    private BCryptPasswordEncoder encoder;

    public JSONObject login(LoginData user) {
        JSONObject returnJson = new JSONObject();
        String userName = user.getUserName();
        Customer dataBaseUser = customerRepository.findCustomerByUserName(userName);

        if (!encoder.matches(user.getUserPassword(), dataBaseUser.getUserPassword())) {
            returnJson.put("message", "密码错误");
        } else {
            //在这里生成token
            String token = JWT.create()
                    .withClaim("userName", userName)
                    .withClaim("userId", dataBaseUser.getUserId())
                    .withClaim("roleId", dataBaseUser.getUserRoleId())
                    .withClaim("expireTime", new Date().getTime())
                    .sign(Algorithm.HMAC256(dataBaseUser.getUserPassword()));
            returnJson.put("token", token);
            returnJson.put("message", "登陆成功");
            dataBaseUser.setUserPassword("*");//不返回用户密码
            returnJson.put("user", dataBaseUser);

        }
        return returnJson;
    }

    public JSONObject signUp(SignUpData signUpData) {
        JSONObject jsonObject = new JSONObject();
        Customer user = new Customer(signUpData);
        if (customerRepository.countCustomerByUserName(user.getUserName()) != 0) {
            jsonObject.put("message", "重名");
            return jsonObject;
        }
        user.setUserPassword(encoder.encode(user.getUserPassword()));
        customerRepository.save(user);
        jsonObject.put("message", "注册成功");
        user.setUserPassword("*");
        jsonObject.put("userDetail", user);
        return jsonObject;
    }

    public JSONObject countUserName(String userName) {
        JSONObject jsonObject = new JSONObject();
        int count = customerRepository.countCustomerByUserName(userName);
        jsonObject.put("count", count);
        return jsonObject;
    }

    public JSONObject updateUserDetail(long customerId, String changeItem, String newValue) {
        Customer user = customerRepository.findCustomerByUserId(customerId);
        String oldValue = "";
        if ("userPassword".equals(changeItem)) {
            oldValue = user.getUserPassword();
            user.setUserPassword(encoder.encode(newValue));
        }
        customerRepository.save(user);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", "更改成功");
        jsonObject.put("oldValue", oldValue);
        jsonObject.put("newValue", newValue);
        return jsonObject;
    }

    public JSONObject initServer() {
        JSONObject result = new JSONObject();
        // queue,pile,vehicle,bill,report
//        result.put("vehicle", chargeService.initVehicle());
//        result.put("queue", chargeService.initQueue());
//        result.put("pile", pileService.initPile());
        result.put("bill", billService.initBill());
//        result.put("report", reportService.initReport());
        return result;
    }
}
